package com.netbetter.langzheng.plugin.fileManager.widget.commandhandle;

import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.Response;
import com.netbetter.langzheng.plugin.fileManager.contents.CommandEnums;
import com.netbetter.langzheng.plugin.fileManager.context.ChangeListItemEventContext;
import com.netbetter.langzheng.plugin.fileManager.context.ChangeTreeItemEventContext;
import com.netbetter.langzheng.plugin.fileManager.context.CopyFileEventContext;
import com.netbetter.langzheng.plugin.fileManager.context.CutFileEventContext;
import com.netbetter.langzheng.plugin.fileManager.context.TableEventContext;
import com.netbetter.langzheng.plugin.fileManager.dto.CommandDTO;
import com.netbetter.langzheng.plugin.fileManager.dto.PasteCommandDTO;
import com.netbetter.langzheng.plugin.fileManager.model.FileModel;
import com.netbetter.langzheng.plugin.fileManager.reader.CommandReader;
import com.netbetter.langzheng.plugin.fileManager.reader.CommandRequestCallback;

/**
 * @author 嘉陵江里捉鱼 email:tang7259@163.com
 *@version 1.0
 *@time Oct 1, 2008 10:38:41 PM
 */
public abstract class CommandHandle {

    public abstract CommandEnums getCommandType();

    public abstract boolean isNormale();

    private CommandDTO getCommandDTO() {
        CommandEnums type = getCommandType();
        if (type.equals(CommandEnums.FILE_PASTE_BY_COPY)) {
            FileModel fileModel = CopyFileEventContext.getFileModel();
            FileModel aimFileModel = ChangeTreeItemEventContext.getFileModel();
            PasteCommandDTO dto = new PasteCommandDTO();
            dto.setType(getCommandType());
            dto.setOriFileModel(fileModel);
            dto.setAimFileModel(aimFileModel);
            return dto;
        } else if (type.equals(CommandEnums.FILE_PASTE_BY_CUT)) {
            FileModel fileModel = CutFileEventContext.getFileModel();
            FileModel aimFileModel = ChangeTreeItemEventContext.getFileModel();
            PasteCommandDTO dto = new PasteCommandDTO();
            dto.setType(getCommandType());
            dto.setOriFileModel(fileModel);
            dto.setAimFileModel(aimFileModel);
            return dto;
        } else if (type.equals(CommandEnums.FILE_DELETE)) {
            FileModel fileModel = ChangeListItemEventContext.getFileModel();
            FileModel aimFileModel = ChangeListItemEventContext.getFileModel();
            PasteCommandDTO dto = new PasteCommandDTO();
            dto.setType(getCommandType());
            dto.setOriFileModel(fileModel);
            dto.setAimFileModel(aimFileModel);
            return dto;
        }

        return null;
    }

    public boolean handleCreateFolder(String aimName) {
        // 发现本级目录存在相同名称不能粘贴
        if (TableEventContext.isExistByName(aimName)) {
            return false;
        }
        FileModel fileModel = new FileModel();
        fileModel.setId(0);
        fileModel.setName(aimName);
        FileModel aimFileModel = ChangeTreeItemEventContext.getFileModel();
        PasteCommandDTO dto = new PasteCommandDTO();
        dto.setType(getCommandType());
        dto.setOriFileModel(fileModel);
        dto.setAimFileModel(aimFileModel);
        CommandReader.exce(dto, new CommandRequestCallback() {

            @Override
            public void onError(Request request, Throwable exception) {
                super.onError(request, exception);
            }

            @Override
            public void onResponseReceived(Request request, Response response) {
                super.onResponseReceived(request, response);
                ChangeTreeItemEventContext.getLoader().loadChildren(ChangeTreeItemEventContext.getFileModel());
            }

        });
        return true;
    }

    public boolean handleModifyName(String aimName) {
        FileModel fileModel = ChangeListItemEventContext.getFileModel();
        if (fileModel != null) {
            if (fileModel.getName().equalsIgnoreCase(aimName)) {
                return true;
            }
            // 发现本级目录存在相同名称不能粘贴
            if (TableEventContext.isExistByName(aimName)) {
                return false;
            }
            FileModel aimFileModel = new FileModel();
            aimFileModel.setId(fileModel.getId());
            aimFileModel.setSize(fileModel.getSize());
            aimFileModel.setName(aimName);
            final PasteCommandDTO dto = new PasteCommandDTO();
            dto.setType(getCommandType());
            dto.setOriFileModel(fileModel);
            dto.setAimFileModel(aimFileModel);
            CommandReader.exce(dto, new CommandRequestCallback() {

                @Override
                public void onError(Request request, Throwable exception) {
                    super.onError(request, exception);
                }

                @Override
                public void onResponseReceived(Request request, Response response) {
                    super.onResponseReceived(request, response);
                    if (dto.getOriFileModel().getIsDirectory()) {
                        ChangeTreeItemEventContext.getLoader().loadChildren(ChangeTreeItemEventContext.getFileModel());
                    }
                }

            });
            return true;
        }
        return false;
    }

    public boolean handle() {
        if (isNormale()) {
            CommandDTO dto = getCommandDTO();
            if (dto != null) {
                CommandReader.exce(getCommandDTO(), new CommandRequestCallback() {

                    @Override
                    public void onError(Request request, Throwable exception) {
                        super.onError(request, exception);
                    }

                    @Override
                    public void onResponseReceived(Request request, Response response) {
                        super.onResponseReceived(request, response);
                    }

                });
            }
        } else {
            return false;
        }
        return true;
    }
}
